//
//  ProductDetailsViewController.m
//  RainbowFM
//
//  Created by Kratos on 14/12/17.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "ProductDetailsViewController.h"
#import "PhotosViewController.h"

#import "GoodsAttributesCell.h"
#import "GoodsPriceCell.h"
#import "GoodsSynopsisCell.h"
#import <SwipeView/SwipeView.h>

#import "GoodsAttributeItem.h"
#import "GoodsCatalogModel.h"

#import "NSString+Size.h"
#import "UserModel.h"

#import "OrderCreatViewController.h"
#import "LoginModel.h"

@interface ProductDetailsViewController ()<SwipeViewDataSource,SwipeViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic , weak) IBOutlet UITableView *tableview;
@property (nonatomic , strong) SwipeView *swipeView;

@property (nonatomic , strong) GoodsAttributeItem *goodsModel;
@end

@implementation ProductDetailsViewController
-(void)awakeFromNib
{
    _goodsModel = [GoodsAttributeItem new];
    [_goodsModel test];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatSwipeView];
    
    typeof(self) self_ = self;
    [[GoodsCatalogModel sharedModel] fetchGoodsDetailWithProductId:_productid result:^(GoodsAttributeItem *item, NSString *msg) {
        if (item) {
            self_.goodsModel = item;
            [self_.swipeView reloadData];
            [self_.tableview reloadData];
        }
    }];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)creatSwipeView
{
    _swipeView = [[SwipeView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 300.f)];
    [_swipeView setBackgroundColor:[UIColor whiteColor]];
    _swipeView.dataSource = self;
    _swipeView.delegate = self;
    [_tableview setTableHeaderView:_swipeView];
}

#pragma mark -SwipeViewDataSource
- (NSInteger)numberOfItemsInSwipeView:(SwipeView *)swipeView
{
    return [_goodsModel.imgsList count];
}

- (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    RFMImageView *showImageView = (RFMImageView *)view;
    if (!showImageView) {
        showImageView = [[RFMImageView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_swipeView.bounds), CGRectGetHeight(_swipeView.bounds))];
        [showImageView setBackgroundColor:[UIColor clearColor]];
        view = showImageView;
    }
    
    NSDictionary *imageDic = _goodsModel.imgsList[index];
    [showImageView.imageView sd_setImageWithURL:imageDic[IMGS_DIC_URL] placeholderImage:nil];
    
    return view;
}

#pragma mark - SwipeViewDelegate
- (void)swipeViewCurrentItemIndexDidChange:(SwipeView *)swipeView
{
    
}

- (void)swipeView:(SwipeView *)swipeView didSelectItemAtIndex:(NSInteger)index
{
    NSMutableArray *images = [NSMutableArray array];
    for (NSDictionary *dic in _goodsModel.imgsList) {
        [images addObject:dic[IMGS_DIC_URL]];
    }
    
    PhotosViewController *photosViewController = [PhotosViewController new];
    [photosViewController setImageUrls:images];
    [self.navigationController pushViewController:photosViewController animated:YES];
}


#pragma mark - table datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 2;
            break;
        case 1:
            return 2;
            break;
        default:
            return 0;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    CGFloat h = 0;
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                    h = 80.f;
                    break;
                case 1:
                    h = [GoodsAttributesCell getHeight:_goodsModel.propertyList];
                    break;
                default:
                    break;
            }
        }
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                    h = 40.f;
                    break;
                case 1:
                {
                    NSString *synopsis = _goodsModel.PRODUCT_DESC ? _goodsModel.PRODUCT_DESC :@" " ;
                    CGSize size = [synopsis calculateSize:CGSizeMake(CGRectGetWidth(self.view.bounds), 1000) font:[UIFont systemFontOfSize:14]];
                    h = size.height > 80.f ? size.height : 80.f;
                }
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }

    return h+1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 0:
                {
                    GoodsPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsPriceCell"];
                    if (!cell) {
                        cell = _LoadXib(GoodsPriceCell, nil, nil);
                    }
                    [cell updateGoodsInfo:_goodsModel];
                    return cell;
                }
                    break;
                case 1:
                {
                    GoodsAttributesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsAttributesCell"];
                    if (!cell) {
                        cell = _LoadXib(GoodsAttributesCell, nil, nil);
                    }
                    [cell updateGoodsInfo:_goodsModel];
                    return cell;
                }
                    break;
                default:
                    return nil;
                    break;
            }
        }
            break;
        case 1:
        {
            switch (indexPath.row) {
                case 0:
                {
                    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsCell"];
                    if (!cell) {
                        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GoodsCell"];
                    }
                    
                    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:_goodsModel.MERCHANT_IMG_URL]];
                    [cell.textLabel setText:_goodsModel.MERCHANT_NAME];

                    return cell;
                }
                    break;
                case 1:
                {
                    GoodsSynopsisCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GoodsSynopsisCell"];
                    if (!cell) {
                        cell = _LoadXib(GoodsSynopsisCell, nil, nil);
                    }
                    [cell updateGoodsInfo:_goodsModel];
                    return cell;
                }
                    break;
                default:
                    return nil;
                    break;
            }
        }
            break;
        default:
            return nil;
            break;
    }
    
}



#pragma mark - table delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (IBAction)goToOrderAction:(id)sender {
    if ([LoginModel sharedModel].userID == nil) {
        UINavigationController *controller = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateInitialViewController];
        [self presentViewController:controller animated:YES completion:nil];
    }else{
        OrderCreatViewController *orderCreate = [self.storyboard instantiateViewControllerWithIdentifier:@"OrderCreatViewController"];
        orderCreate.productItem = self.goodsModel;
        [self.navigationController pushViewController:orderCreate animated:YES];
    }
    
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//    if ([[segue identifier] isEqualToString:@"order"]) {
//        if ([UserModel sharedModel].userInfo !=nil) {
//            
//        }
//    }
//}


@end
